package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.AdTopic;
import com.abaaba.project2.bean.AdTopicExample;
import com.abaaba.project2.bean.bo.AdTopicReqBo;
import com.abaaba.project2.bean.bo.AddAdvertisementTopicBo;
import com.abaaba.project2.bean.bo.BaseReqBo;
import com.abaaba.project2.bean.bo.UpdateAdvertisementTopicBo;
import com.abaaba.project2.bean.data.AdvertisementInfo;
import com.abaaba.project2.bean.data.AdvertisementTopicInfo;
import com.abaaba.project2.bean.vo.AddAdvertisementTopicVo;
import com.abaaba.project2.bean.vo.AdvertisementTopicListVo;
import com.abaaba.project2.bean.vo.BaseRespVo;
import com.abaaba.project2.mapper.AdTopicMapper;
import com.abaaba.project2.service.AdvertisementTopicService;
import com.abaaba.project2.util.DateToStringUtil;
import com.abaaba.project2.util.PageHelperUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdvertisementTopicServiceImpl implements AdvertisementTopicService {

    @Autowired
    AdTopicMapper adTopicMapper;

    @Override
    public AdvertisementTopicListVo queryTopic(AdTopicReqBo baseReqBo) {

        //这里做判断：根据是否包含title、subtitle来判断查询方式
        String title = baseReqBo.getTitle();
        String subtitle = baseReqBo.getSubtitle();

        AdvertisementTopicListVo advertisementTopicListVo = null;

        if (title == null && subtitle == null) {
            advertisementTopicListVo = queryAllTopic(baseReqBo);
        } else {
            advertisementTopicListVo = queryTopicByTitleAndSubTitle(baseReqBo);
        }

        return advertisementTopicListVo;
    }

    @Override
    public AddAdvertisementTopicVo createAdTopic(AddAdvertisementTopicBo addAdvertisementTopicBo) {
        //直接插入即可

        //创建example对象
        AdTopicExample adTopicExample = new AdTopicExample();
        AdTopicExample.Criteria criteria = adTopicExample.createCriteria();

        AdTopic adTopic = new AdTopic();
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        adTopic.setAddTime(date);
        adTopic.setUpdateTime(date);
        adTopic.setContent(addAdvertisementTopicBo.getContent());
        adTopic.setGoods(addAdvertisementTopicBo.getGoods());
        adTopic.setPicUrl(addAdvertisementTopicBo.getPicUrl());
        //price这里注意：前端请求传来的是字符串，这里要转化为BigDemical类型
        String price = addAdvertisementTopicBo.getPrice();
        BigDecimal price1 = new BigDecimal(price);
        adTopic.setPrice(price1);
        adTopic.setReadCount(addAdvertisementTopicBo.getReadCount());
        adTopic.setSubtitle(addAdvertisementTopicBo.getSubtitle());
        adTopic.setTitle(addAdvertisementTopicBo.getTitle());

        //将记录插入到数据库中（由于要返回主键id，这里需要对xml进行修改）
        adTopicMapper.insertSelective(adTopic);

        AddAdvertisementTopicVo addAdvertisementTopicVo = new AddAdvertisementTopicVo();
        addAdvertisementTopicVo.setAddTime(DateToStringUtil.dateToString(date));
        addAdvertisementTopicVo.setUpdateTime(DateToStringUtil.dateToString(date));
        addAdvertisementTopicVo.setContent(addAdvertisementTopicBo.getContent());
        addAdvertisementTopicVo.setGoods(addAdvertisementTopicBo.getGoods());
        addAdvertisementTopicVo.setId(adTopic.getId());
        addAdvertisementTopicVo.setPicUrl(addAdvertisementTopicBo.getPicUrl());
        addAdvertisementTopicVo.setPrice(adTopic.getPrice());
        addAdvertisementTopicVo.setReadCount(adTopic.getReadCount());
        addAdvertisementTopicVo.setSubtitle(addAdvertisementTopicBo.getSubtitle());
        addAdvertisementTopicVo.setTitle(addAdvertisementTopicBo.getTitle());

        return addAdvertisementTopicVo;
    }

    @Override
    public AdvertisementTopicInfo updateAdTopic(UpdateAdvertisementTopicBo updateAdvertisementTopicBo) {

        //创建example对象
        AdTopicExample adTopicExample = new AdTopicExample();
        AdTopicExample.Criteria criteria = adTopicExample.createCriteria();

        AdTopic adTopic = new AdTopic();
        adTopic.setId(updateAdvertisementTopicBo.getId());
        adTopic.setTitle(updateAdvertisementTopicBo.getTitle());
        adTopic.setSubtitle(updateAdvertisementTopicBo.getSubtitle());
        adTopic.setPicUrl(updateAdvertisementTopicBo.getPicUrl());
        adTopic.setContent(updateAdvertisementTopicBo.getContent());

        //注意：这里需要获取更新时间
        Date date1 = new Date(System.currentTimeMillis());
        adTopic.setUpdateTime(date1);
        //这里要进行数据格式转换：string --> BigDemical
        String price = updateAdvertisementTopicBo.getPrice();
        adTopic.setPrice(new BigDecimal(price));
        adTopic.setReadCount(updateAdvertisementTopicBo.getReadCount());

        //拼接sql语句：根据id进行更新
        adTopicMapper.updateByPrimaryKeySelective(adTopic);

        //从数据库中查询出更新后的记录
        AdTopic adTopic1 = adTopicMapper.selectByPrimaryKey(adTopic.getId());

        AdvertisementTopicInfo advertisementTopicInfo = new AdvertisementTopicInfo();
        //Date-->String
        Date date2 = adTopic1.getAddTime();
        Date date3 = adTopic1.getUpdateTime();

        String addTime = DateToStringUtil.dateToString(date2);
        String updateTime = DateToStringUtil.dateToString(date3);

        advertisementTopicInfo.setAddTime(addTime);
        advertisementTopicInfo.setUpdateTime(updateTime);
        advertisementTopicInfo.setContent(adTopic1.getContent());
        advertisementTopicInfo.setDeleted(adTopic1.getDeleted());
        advertisementTopicInfo.setGoods(adTopic1.getGoods());
        advertisementTopicInfo.setId(adTopic1.getId());
        advertisementTopicInfo.setPicUrl(adTopic1.getPicUrl());
        advertisementTopicInfo.setPrice(adTopic1.getPrice());
        advertisementTopicInfo.setReadCount(adTopic1.getReadCount());
        advertisementTopicInfo.setSortOrder(adTopic1.getSortOrder());
        advertisementTopicInfo.setSubtitle(adTopic1.getSubtitle());
        advertisementTopicInfo.setTitle(adTopic1.getTitle());

        return advertisementTopicInfo;
    }

    @Override
    public void deleteAdTopic(AdvertisementTopicInfo advertisementTopicInfo) {

        //根据id逻辑删除：更新deleted=1即可
        AdTopicExample adTopicExample = new AdTopicExample();
        AdTopicExample.Criteria criteria = adTopicExample.createCriteria();
        //拼接sql语句
        criteria.andIdEqualTo(advertisementTopicInfo.getId());
        AdTopic adTopic = new AdTopic();
        adTopic.setId(advertisementTopicInfo.getId());
        adTopic.setDeleted(true);
        adTopicMapper.updateByPrimaryKeySelective(adTopic);
    }

    private AdvertisementTopicListVo queryTopicByTitleAndSubTitle(AdTopicReqBo baseReqBo) {

        ///使用工具类PageHelper进行分页查询
        PageHelperUtil.startPage(baseReqBo);

        //查询条件：deleted = 0（没有被删除的记录）并且根据title和subtitle进行模糊查询
        //创建example对象
        AdTopicExample adTopicExample = new AdTopicExample();
        AdTopicExample.Criteria criteria = adTopicExample.createCriteria();

        String title = baseReqBo.getTitle();
        String subtitle = baseReqBo.getSubtitle();
        //拼接sql语句
        if (title != null && !"".equals(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (subtitle != null && !"".equals(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        criteria.andDeletedEqualTo(false);

        //排序信息
        adTopicExample.setOrderByClause(baseReqBo.getSort() + " " + baseReqBo.getOrder());

        //这里选择查询方法时要注意
        List<AdTopic> adTopics = adTopicMapper.selectByExampleWithBLOBs(adTopicExample);

        //获得分页信息
        PageInfo<AdTopic> adTopicPageInfo = new PageInfo<>(adTopics);
        long total = adTopicPageInfo.getTotal();

        AdvertisementTopicListVo advertisementTopicListVo = new AdvertisementTopicListVo();
        advertisementTopicListVo.setTotal(total);

        List<AdvertisementTopicInfo> advertisementTopicInfos = new ArrayList<>();

        for (AdTopic adTopic : adTopics) {
            AdvertisementTopicInfo advertisementTopicInfo = new AdvertisementTopicInfo();
            advertisementTopicInfo.setId(adTopic.getId());
            advertisementTopicInfo.setTitle(adTopic.getTitle());
            advertisementTopicInfo.setSubtitle(adTopic.getSubtitle());
            advertisementTopicInfo.setPrice(adTopic.getPrice());
            advertisementTopicInfo.setReadCount(adTopic.getReadCount());
            advertisementTopicInfo.setPicUrl(adTopic.getPicUrl());
            advertisementTopicInfo.setSortOrder(adTopic.getSortOrder());

            //利用日期转换工具类
            Date date1 =  adTopic.getAddTime();
            String addTime = DateToStringUtil.dateToString(date1);
            advertisementTopicInfo.setAddTime(addTime);

            Date date2 = adTopic.getUpdateTime();
            String updateTime = DateToStringUtil.dateToString(date2);
            advertisementTopicInfo.setUpdateTime(updateTime);

            advertisementTopicInfo.setDeleted(adTopic.getDeleted());
            advertisementTopicInfo.setContent(adTopic.getContent());
            advertisementTopicInfo.setGoods(adTopic.getGoods());

            advertisementTopicInfos.add(advertisementTopicInfo);
        }
        advertisementTopicListVo.setItems(advertisementTopicInfos);

        return advertisementTopicListVo;
    }

    private AdvertisementTopicListVo queryAllTopic(AdTopicReqBo baseReqBo) {
        //后面注意topic表中的goods为字符串类型，而Vo对象中的AdvertisementTopicInfo中的成员变量goods为数组类型
        // 应该采用TypeHandler进行转换（string-->string[]）

        ///使用工具类PageHelper进行分页查询
        PageHelperUtil.startPage(baseReqBo);

        //查询条件：deleted = 0（没有被删除的记录）
        //创建example对象
        AdTopicExample adTopicExample = new AdTopicExample();
        AdTopicExample.Criteria criteria = adTopicExample.createCriteria();

        //排序信息,这个操作要在查询之前进行
        adTopicExample.setOrderByClause(baseReqBo.getSort() + " " + baseReqBo.getOrder());

        //拼接sql语句
        criteria.andDeletedEqualTo(false);
        List<AdTopic> adTopics = adTopicMapper.selectByExampleWithBLOBs(adTopicExample);

        //获得分页信息
        PageInfo<AdTopic> adTopicPageInfo = new PageInfo<>(adTopics);
        long total = adTopicPageInfo.getTotal();

        AdvertisementTopicListVo advertisementTopicListVo = new AdvertisementTopicListVo();
        advertisementTopicListVo.setTotal(total);

        List<AdvertisementTopicInfo> advertisementTopicInfos = new ArrayList<>();

        for (AdTopic adTopic : adTopics) {
            AdvertisementTopicInfo advertisementTopicInfo = new AdvertisementTopicInfo();
            advertisementTopicInfo.setId(adTopic.getId());
            advertisementTopicInfo.setTitle(adTopic.getTitle());
            advertisementTopicInfo.setSubtitle(adTopic.getSubtitle());
            advertisementTopicInfo.setPrice(adTopic.getPrice());
            advertisementTopicInfo.setReadCount(adTopic.getReadCount());
            advertisementTopicInfo.setPicUrl(adTopic.getPicUrl());
            advertisementTopicInfo.setSortOrder(adTopic.getSortOrder());

            //利用日期转换工具类
            Date date1 =  adTopic.getAddTime();
            String addTime = DateToStringUtil.dateToString(date1);
            advertisementTopicInfo.setAddTime(addTime);

            Date date2 = adTopic.getUpdateTime();
            String updateTime = DateToStringUtil.dateToString(date2);
            advertisementTopicInfo.setUpdateTime(updateTime);

            advertisementTopicInfo.setDeleted(adTopic.getDeleted());
            advertisementTopicInfo.setContent(adTopic.getContent());
            advertisementTopicInfo.setGoods(adTopic.getGoods());

            advertisementTopicInfos.add(advertisementTopicInfo);
        }
        advertisementTopicListVo.setItems(advertisementTopicInfos);
        return advertisementTopicListVo;
    }
}
